// import { getColor } from '@/utils/editor/defaultStyleUtil';
import { linkNode } from '@/api/admin/systemConfig/itRoad/index';

export default function (G6: any) {
	// Register a custom behavior: click two end nodes to add an edge
	G6.registerBehavior('click-add-edge', {
		// Set the events and the corresponding responsing function for this behavior
		getEvents() {
			return {
				'node:click': 'onClick', // The event is canvas:click, the responsing function is onClick
				mousemove: 'onMousemove', // The event is mousemove, the responsing function is onMousemove
				click: 'onEdgeClick', // The event is edge:click, the responsing function is onEdgeClick
			};
		},
		// 节点单击事件
		onClick(ev: any) {
			const self = this as any;
			const graph = self.graph;
			const clickShape = ev.target;
			const model = ev.item.getModel();
			const point = { x: ev.x, y: ev.y };

			if (self.addingEdge && self.edge) {
				let flag = true;
				const edgeSourceNodeModel = self.edge.getSource().getModel();
				const edges = graph.getEdges();

				for (let i = 0, len = edges.length - 1; i < len; i++) {
					const sNode = edges[i].getSource();
					const tNode = edges[i].getTarget();
					if (sNode._cfg && tNode._cfg) {
						const sourceNodeModelId = sNode.getModel().id;
						const targetNodeModelId = tNode.getModel().id;

						if (edgeSourceNodeModel.id === sourceNodeModelId && model.id === targetNodeModelId) {
							flag = false;
							break;
						}
					}
				}

				if (flag) {
					graph.updateItem(this.edge, {
						target: model.id,
					});
					linkNode({
						systemItBeforeId: self.edge.getSource().getModel().id,
						systemItId: model.id,
					}).then((res: any) => {
						if (res.code === 0) {
							console.log(res, 'res');
						}
					});
					/*}else{
            // 两节点之间已存在连线
            self.graph.removeItem(self.edge);
          }*/

					this.edge = null;
					this.addingEdge = false;
				}
			} else {
				if (clickShape.cfg.name === 'rightCircleShape' || clickShape.cfg.name === 'leftCircleShape') {
					self.edge = graph.addItem('edge', {
						type: 'myCubicHorizontal',
						source: model.id,
						target: point,
						data: {
							nodeType: model.nodeType,
						},
					});
					self.addingEdge = true;
				}
			}

			// 点击左右锚点图形才触发连线
			/*if (clickShape.cfg.name === 'rightCircleShape' || clickShape.cfg.name === 'leftCircleShape'){
        if (!self.addingEdge && !self.edge) {
          self.edge = graph.addItem('edge', {
            type: 'myCubicHorizontal',
            source: model.id,
            target: model.id,
            data: {
              nodeType: model.nodeType
            }
          });
          self.addingEdge = true;
          return;
        }
      }
      // 点击节点完成连线，如果节点之间已存在边，则不再创建边
      if(self.addingEdge && self.edge) {
        var flag = true;
        const edgeSourceNodeModel = self.edge.getSource().getModel();
        const edges = graph.getEdges();
        for(let i = 0, len = edges.length - 1; i < len; i++) {
          const sNode = edges[i].getSource();
          const tNode = edges[i].getTarget();
          if (sNode._cfg && tNode._cfg) {
            const sourceNodeModelId = sNode.getModel().id;
            const targetNodeModelId = tNode.getModel().id;
            if(edgeSourceNodeModel.id === sourceNodeModelId && model.id === targetNodeModelId) {
              flag = false;
              break;
            }
          }
        }
        if(flag) {
          console.log(self.edge.getSource());
          console.log(self.edge.getTarget());
          console.log(model);
          graph.updateItem(self.edge, {
            target: model.id,
            style: {
              stroke: getColor(model.nodeType),
            },
          });
          linkNode({
            segmentBeforeId: self.edge.getSource().getModel().id,
            segmentId: model.id
          }).then(res => {
            if (res.code === 200) {

            }
          });
        }else{
					// 两节点之间已存在连线
          self.graph.removeItem(self.edge);
        }
        self.edge = null;
        self.addingEdge = false;
      }*/
		},
		// 鼠标移动事件
		onMousemove(ev: any) {
			const self = this as any;

			console.log(ev, 'evev');
			if (this.addingEdge && this.edge) {
				const point = { x: ev.x, y: ev.y };
				// 更新边的坐标为鼠标当前的坐标
				this.graph.updateItem(this.edge, {
					target: point,
				});
			}
		},
		// 点击事件，点击canvas空白区域取消当前不完整的边
		onEdgeClick(ev: any) {
			const self = this as any;
			const currentEdge = ev.item;
			if (self.addingEdge && self.edge === currentEdge) {
				self.graph.removeItem(self.edge);
				self.edge = null;
				self.addingEdge = false;
			}
		},
	});
}
